const Sequelize = require('sequelize')
const db = require('../db.js')
const CateModel = require('./cate.js')
//把article表的字段映射成对象的属性,封装成Article对象
const Article = db.define(
  'Article',
  {
    id: { type: Sequelize.INTEGER, primaryKey: true, allowNull: false, autoIncrement: true },
    //标题
    title: { type: Sequelize.STRING, allowNull: false },
    //概述
    desc: { type: Sequelize.STRING, allowNull: false },
    //文章封面
    cover: { type: Sequelize.STRING, allowNull: false },
    //文章内容
    content: { type: Sequelize.TEXT, allowNull: false },
    //文章的分类
    cate: { type: Sequelize.INTEGER, allowNull: false },
  },
  {
    //是否支持驼峰命名
    underscored: true,
    //Article对象，对应的数据库表
    tableName: 'article',
  }
)
//设置article表cate字段外键关联cate表的主键，一个分类有多个文章，文章属于某个分类
Article.belongsTo(CateModel, { foreignKey: 'cate', constraints: false })
module.exports = Article
